package com.dizner.dimagepicker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ImagesDirListAdapter extends RecyclerView.Adapter<ImagesDirListAdapter.ViewHolder> {
    private Context mContext;

    private List<ImageDir> mDataList;

    private AdapterView.OnItemClickListener onItemClickListener;

    public ImagesDirListAdapter(Context mContext) {
        this.mContext = mContext;
        mDataList = new ArrayList<>();
    }

    public void setDataList(List<ImageDir> dataList) {
        mDataList.clear();
        if (dataList != null) {
            mDataList.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_images_dir_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        ImageDir dir = mDataList.get(position);
        holder.tvDirName.setText(dir.getDirName());
        holder.tvDirPath.setText(dir.getPath());

//        String extName = Utils.getFileDirExtName(dir.getPath());

//        holder.tvDirExtName.setText(dir.getExtName());

        holder.clItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(null,holder.clItemView,position,position);
                }
            }
        });
    }


    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvDirPath;
        private TextView tvDirName;
        private TextView tvDirExtName;
        private View clItemView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDirPath = itemView.findViewById(R.id.tv_dir_path);
            tvDirName = itemView.findViewById(R.id.tv_dir_name);
            tvDirExtName = itemView.findViewById(R.id.tv_dir_ext_name);
            clItemView = itemView.findViewById(R.id.cl_item_root);
        }
    }
}
